<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="150">
            <el-form-item label="店铺名称" prop="storeName">
              <el-input v-model="queryParams.storeName" placeholder="请输入店铺名称" clearable />
            </el-form-item>
            <el-form-item label="FNSKU" prop="fnSku">
              <el-input v-model="queryParams.fnSku" placeholder="请输入本系统的FNSKU" clearable />
            </el-form-item>
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable />
            </el-form-item>
            <el-form-item label="通用码" prop="universalCode">
              <el-input v-model="queryParams.universalCode" placeholder="请输入通用码" clearable />
            </el-form-item>
            <el-form-item label="erp的SKU" prop="erpSku">
              <el-input v-model="queryParams.erpSku" placeholder="请输入erp的SKU" clearable />
            </el-form-item>
            <el-form-item label="erp的产品名称" prop="erpProductName">
              <el-input v-model="queryParams.erpProductName" placeholder="请输入erp的产品名称" clearable />
            </el-form-item>
            <el-form-item label="老的物料编码" prop="materialCodeOld">
              <el-input v-model="queryParams.materialCodeOld" placeholder="请输入老的物料编码" clearable />
            </el-form-item>
            <el-form-item label="新的物料编码" prop="materialCodeNew">
              <el-input v-model="queryParams.materialCodeNew" placeholder="请输入新的物料编码" clearable />
            </el-form-item>
            <el-form-item label="第三方平台父编码" prop="thirdSalePlatformParentCode">
              <el-input
                v-model="queryParams.thirdSalePlatformParentCode"
                placeholder="请输入第三方销售平台父编码(temu的sku,亚马逊的父ASIN)"
                clearable
              />
            </el-form-item>
            <el-form-item label="第三方平台子编码" prop="thirdSalePlatformSubCode">
              <el-input v-model="queryParams.thirdSalePlatformSubCode" placeholder="请输入第三方销售平台子编码(temu的spu,亚马逊的子ASIN)" clearable />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['product:productMultiDimensionalMapping:add']" type="primary" plain icon="Plus" @click="handleAdd"
              >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['product:productMultiDimensionalMapping:edit']"
              type="success"
              plain
              icon="Edit"
              :disabled="single"
              @click="handleUpdate()"
              >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['product:productMultiDimensionalMapping:remove']"
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete()"
              >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['product:productMultiDimensionalMapping:export']" type="warning" plain icon="Download" @click="handleExport"
              >导出
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain @click="handleGen">生成数据(增量生成)</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="productMultiDimensionalMappingList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="" align="center" prop="id" />
        <el-table-column label="店铺ID" align="center" prop="storeId" />
        <el-table-column label="店铺名称" align="center" prop="storeName" />
        <el-table-column label="本系统的FNSKU" align="center" prop="fnSku" />
        <el-table-column label="产品名称" align="center" prop="productName" />
        <el-table-column label="产品类型" align="center" prop="productType">
          <template #default="scope">{{ product_type.find((item) => item.value === scope.row.productType)?.label }} </template>
        </el-table-column>
        <el-table-column label="通用码" align="center" prop="universalCode" />
        <el-table-column label="erp的SKU" align="center" prop="erpSku" />
        <el-table-column label="erp的产品名称" align="center" prop="erpProductName" />
        <el-table-column label="老的物料编码" align="center" prop="materialCodeOld" />
        <el-table-column label="新的物料编码" align="center" prop="materialCodeNew" />
        <el-table-column label="第三方平台父编码" align="center" prop="thirdSalePlatformParentCode" />
        <el-table-column label="第三方平台子编码" align="center" prop="thirdSalePlatformSubCode" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                v-hasPermi="['product:productMultiDimensionalMapping:edit']"
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                v-hasPermi="['product:productMultiDimensionalMapping:remove']"
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改产品多维度映射对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" append-to-body>
      <el-form ref="productMultiDimensionalMappingFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺ID" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入店铺ID" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="本系统的FNSKU" prop="fnSku">
          <el-input v-model="form.fnSku" placeholder="请输入本系统的FNSKU" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="通用码" prop="universalCode">
          <el-input v-model="form.universalCode" placeholder="请输入通用码" />
        </el-form-item>
        <el-form-item label="erp的SKU" prop="erpSku">
          <el-input v-model="form.erpSku" placeholder="请输入erp的SKU" />
        </el-form-item>
        <el-form-item label="erp的产品名称" prop="erpProductName">
          <el-input v-model="form.erpProductName" placeholder="请输入erp的产品名称" />
        </el-form-item>
        <el-form-item label="老的物料编码" prop="materialCodeOld">
          <el-input v-model="form.materialCodeOld" placeholder="请输入老的物料编码" />
        </el-form-item>
        <el-form-item label="新的物料编码" prop="materialCodeNew">
          <el-input v-model="form.materialCodeNew" placeholder="请输入新的物料编码" />
        </el-form-item>
        <el-form-item label="第三方平台父编码" prop="thirdSalePlatformParentCode">
          <el-input v-model="form.thirdSalePlatformParentCode" placeholder="请输入第三方销售平台父编码(temu的sku,亚马逊的父ASIN)" />
        </el-form-item>
        <el-form-item label="第三方平台子编码" prop="thirdSalePlatformSubCode">
          <el-input v-model="form.thirdSalePlatformSubCode" placeholder="请输入第三方销售平台子编码(temu的spu,亚马逊的子ASIN)" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ProductMultiDimensionalMapping" lang="ts">
import {
  listProductMultiDimensionalMapping,
  getProductMultiDimensionalMapping,
  delProductMultiDimensionalMapping,
  addProductMultiDimensionalMapping,
  updateProductMultiDimensionalMapping,
  genData
} from '@/api/product/productMultiDimensionalMapping';
import {
  ProductMultiDimensionalMappingVO,
  ProductMultiDimensionalMappingQuery,
  ProductMultiDimensionalMappingForm
} from '@/api/product/productMultiDimensionalMapping/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { product_type } = toRefs<any>(proxy?.useDict('product_type'));

const productMultiDimensionalMappingList = ref<ProductMultiDimensionalMappingVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const productMultiDimensionalMappingFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: ProductMultiDimensionalMappingForm = {
  id: undefined,
  storeId: undefined,
  storeName: undefined,
  fnSku: undefined,
  productName: undefined,
  productType: undefined,
  universalCode: undefined,
  erpSku: undefined,
  erpProductName: undefined,
  materialCodeOld: undefined,
  materialCodeNew: undefined,
  thirdSalePlatformParentCode: undefined,
  thirdSalePlatformSubCode: undefined,
  remark: undefined
};
const data = reactive<PageData<ProductMultiDimensionalMappingForm, ProductMultiDimensionalMappingQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    storeId: undefined,
    storeName: undefined,
    fnSku: undefined,
    productName: undefined,
    productType: undefined,
    universalCode: undefined,
    erpSku: undefined,
    erpProductName: undefined,
    materialCodeOld: undefined,
    materialCodeNew: undefined,
    thirdSalePlatformParentCode: undefined,
    thirdSalePlatformSubCode: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '不能为空', trigger: 'blur' }],
    storeId: [{ required: true, message: '店铺ID不能为空', trigger: 'blur' }],
    storeName: [{ required: true, message: '店铺名称不能为空', trigger: 'blur' }],
    fnSku: [{ required: true, message: '本系统的FNSKU不能为空', trigger: 'blur' }],
    productName: [{ required: true, message: '产品名称不能为空', trigger: 'blur' }],
    productType: [{ required: true, message: '产品类型(普通产品，组合产品)字典不能为空', trigger: 'change' }],
    universalCode: [{ required: true, message: '通用码不能为空', trigger: 'blur' }],
    erpSku: [{ required: true, message: 'erp的SKU不能为空', trigger: 'blur' }],
    erpProductName: [{ required: true, message: 'erp的产品名称不能为空', trigger: 'blur' }],
    materialCodeOld: [{ required: true, message: '老的物料编码不能为空', trigger: 'blur' }],
    materialCodeNew: [{ required: true, message: '新的物料编码不能为空', trigger: 'blur' }],
    thirdSalePlatformParentCode: [{ required: true, message: '第三方销售平台父编码(temu的sku,亚马逊的父ASIN)不能为空', trigger: 'blur' }],
    thirdSalePlatformSubCode: [{ required: true, message: '第三方销售平台子编码(temu的spu,亚马逊的子ASIN)不能为空', trigger: 'blur' }],
    remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询产品多维度映射列表 */
const getList = async () => {
  loading.value = true;
  const res = await listProductMultiDimensionalMapping(queryParams.value);
  productMultiDimensionalMappingList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  productMultiDimensionalMappingFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: ProductMultiDimensionalMappingVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加产品多维度映射';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: ProductMultiDimensionalMappingVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getProductMultiDimensionalMapping(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改产品多维度映射';
};

/** 提交按钮 */
const submitForm = () => {
  productMultiDimensionalMappingFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateProductMultiDimensionalMapping(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addProductMultiDimensionalMapping(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: ProductMultiDimensionalMappingVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除产品多维度映射编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delProductMultiDimensionalMapping(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'product/productMultiDimensionalMapping/export',
    {
      ...queryParams.value
    },
    `productMultiDimensionalMapping_${new Date().getTime()}.xlsx`
  );
};

/** 提交按钮操作 */
const handleGen = async () => {
  loading.value = true;
  await proxy?.$modal.confirm('是否确认提交？').finally(() => (loading.value = false));
  loading.value = true;
  await genData(queryParams.value).finally(() => (loading.value = false));
  proxy?.$modal.msgSuccess('提交成功');
  await getList();
};

onMounted(() => {
  getList();
});
</script>
